﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageAdmin
{
    public partial class PageAddStaff : System.Web.UI.Page
    {
        private CNhanVien _cnv = new CNhanVien();
        private bool _flag = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            pnThongBao.Visible = false;
            lbThongBao.Text = "";
        }

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            if (_flag)
            {
                NhanVien nv = new NhanVien();
                nv.Email = txtEmail.Text;
                if(rblGender.SelectedValue != "-1"){
                    nv.GioiTinh = Convert.ToBoolean(rblGender.SelectedValue);
                }
                nv.HoTen = txtFirstname.Text;
                nv.LoaiND = Convert.ToInt32(rbtType.SelectedValue);
                nv.MatKhau = "202cb962ac59075b964b07152d234b70";
                if(!txtAddress.Text.Equals("")){
                    nv.DiaChi = txtAddress.Text;
                }
                nv.TinhTrang = true;

                bool kq = _cnv.InsertInfoStaff(nv);
                if (kq)
                {
                    ClearInfo();
                    pnThongBao.Visible = true;
                    pnThongBao.CssClass = "InLeft success message";
                    lbThongBao.Text = "Add employee successfully.";
                }
                else
                {
                    pnThongBao.Visible = true;
                    pnThongBao.CssClass = "InLeft error message";
                    lbThongBao.Text = "Add employee failure.";
                }
            }
        }

        private void ClearInfo()
        {
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtFirstname.Text = "";
            rbtType.SelectedIndex = -1;
            rblGender.SelectedIndex = -1;
        }

        protected void cusvEmail_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string email = txtEmail.Text;
            bool f = _cnv.EmailExistTest(email);
            if (f)
            {
                args.IsValid = true;
            }
            else
            {
                _flag = false;
                args.IsValid = false;
            }
        }
    }
}